package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

public class StudentDao {
    private static Student[] stus = new Student[5];
    //添加学生方法
    public static boolean addStudent(Student stu) {
        //创建学生对象数组
        //添加学生方法
        int index = -1;
        for (int i = 0;i<stus.length;i++){
            Student student= stus [i];
            if (student == null){
                index = i;
                break;
            }
        }
        if (index==-1){
            //装满了
            return false;
        }else {
            //未装满
            stus[index] = stu;
            return true;
        }
    }
    //查看学生方法
    public static Student[] findAllStudent(){
        return  stus;
    }
    public static void deleteStudentById(String delId) {
        //查找id在容器中的索引位置
        int index = getIndex(delId);
        //将该索引位置，使用null元素进行覆盖
        stus[index] = null;
    }
    public static int getIndex(String id){
        int index = -1;
        for (int i = 0; i <stus.length ; i++) {
            Student stu =stus[i];
            if (stu !=null && stu.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }
    public static void updateStudent(String updateId, Student newStu){
        int index = getIndex(updateId);
        stus[index] = newStu;
    }
}